package pkgIfx_Conexiones;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import com.neurotechnology.NImages.NImage;
import com.neurotechnology.NImages.NImageFormat;
import com.neurotechnology.Templates.NFRecord;
import java.sql.SQLException;
import adirector.cIfxDatosTemplate;

public class cIfxBD_Huella {

	private Connection con;
	public cIfxBD_Huella() throws Exception {
		Class.forName("org.gjt.mm.mysql.Driver");
		con = DriverManager.getConnection("jdbc:mysql://localhost:3306/fingers","root","9912056");
	}

	public synchronized void append(String name, NImage image, NFRecord template) throws Exception {

		PreparedStatement pstmt = con.prepareStatement("Select MAX(Id) from Templates");
		int maxID = pstmt.executeQuery().getInt(1);
		
		byte[] pngimage = null;
		for (NImageFormat format : NImageFormat.getFormats()) 
			if (format.getName().contains("PNG"))
				pngimage = format.saveToMemory(image);
		
		pstmt = con.prepareStatement("INSERT INTO Templates VALUES (?,?,?,?)");
		pstmt.setInt(1, maxID + 1);
		if (name != null)
			pstmt.setString(2, name);
		if (template != null)
			pstmt.setBytes(3, template.save());
		if (pngimage != null)
			pstmt.setBytes(4, null);
		pstmt.execute();
	}
        
        public void Borrar_Huella(String cedula) {
		try {
			Statement delete = con.createStatement();
                        delete.executeUpdate("DELETE FROM Templates WHERE TemplateId = '"+cedula+"'");
		} catch (Exception e) {
			System.out.println(e.getMessage());
                        System.out.println(e);
		}
	}
                
     public String[] Face(String cedulaBus){
         String consulta[] = new String[4];
         PreparedStatement stmt = null; //statement para el sql
            try {
                    stmt = con.prepareStatement("SELECT * FROM Templates where TemplateId ='"+cedulaBus+"'");
                    ResultSet rs = stmt.executeQuery();
                    while (rs.next()) {
                        for (int row = 0; row <= 3; row++) {
                            consulta[row] = rs.getString(row+1);
                        }
                    }
            rs.close();
            con.close();
            } catch (Exception e) {
                System.out.println("Error en consulta de cedula BD: "+e);
                return consulta;
            }
            return consulta;
     }

        public cIfxDatosTemplate[] bus_fin_Images(String cedula) throws SQLException {
             cIfxDatosTemplate[] ofceimge = new cIfxDatosTemplate[4];
             PreparedStatement stmt = null; //statement para el sql
            try {
                    stmt = con.prepareStatement("SELECT * FROM Templates where TemplateId ='"+cedula+"'");
                    System.out.println("SELECT * FROM Templates where TemplateId ='"+cedula+"'");
                    ResultSet rs = stmt.executeQuery();
                    int i=0;
                    while (rs.next()) {
                        ofceimge[i] = new cIfxDatosTemplate();
                        ofceimge[i].T_Id = rs.getInt(1);
                        ofceimge[i].T_Template_Id=rs.getString(2);
                        ofceimge[i].T_Template= rs.getBytes(3);
                        ofceimge[i].T_Thumbnail= rs.getBytes(4);
                        i++;
                    }
                    rs.close();
            } catch (Exception e) {
                System.out.println("Error en consulta de cedula BD 3: "+e);
                return ofceimge;
            }
            return ofceimge;
        }
}
